package net.ariel.myideablog.impl;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import net.ariel.myideablog.dao.IBlog;
import net.ariel.myideablog.dao.ICategory;
import net.ariel.myideablog.model.Blog;
import net.ariel.myideablog.model.Category;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class BlogImpl implements IBlog {

	private PersistenceManagerFactory persistenceManagerFactory;
	private ICategory iCategory;
	
	public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) {
		this.persistenceManagerFactory = pmf;
	}
	
	public void setICategory(ICategory iCategory) {
		this.iCategory = iCategory;
	}
	
	@Override
	public List<Blog> getAllBlogs() {
		// TODO Auto-generated method stub
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		String query = "select from "+Blog.class.getName()+" order by createdAt desc";
		List<Blog> blogs = (List<Blog>)pm.newQuery(query).execute();
		for(Blog blog : blogs) {
			Category category = iCategory.getCategory(blog.getCategoryKey());
			blog.setCategory(category);
		}
		return blogs;
	}

	@Override
	public List<Blog> getBlogsByPage(int offset, int limit) {
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		String query = "select from "+Blog.class.getName()+" order by createdAt desc range "+Integer.toString(offset)+", "+Integer.toString(limit);
		List<Blog> blogs = (List<Blog>)pm.newQuery(query).execute();
		for(Blog blog : blogs) {
			Category category = iCategory.getCategory(blog.getCategoryKey());
			blog.setCategory(category);
		}
		return blogs;
	}
	
	@Override
	public void saveBlog(Blog blog) {
		// TODO Auto-generated method stub
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		try {
			Date date = new Date();
			blog.setCreatedAt(date);
			pm.makePersistent(blog);
		}
		finally {
			pm.close();
		}
	}
	
	@Override
	public Blog getBlog(String blogKey) {
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		try {
			return pm.getObjectById(Blog.class, KeyFactory.stringToKey(blogKey));
		}
		finally {
			pm.close();
		}
	}
	
	@Override
	public void updateBlog(String blogKey, Blog newBlog) {
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		try {
			Blog blog = pm.getObjectById(Blog.class, KeyFactory.stringToKey(blogKey));
			blog.setTitle(newBlog.getTitle());
			blog.setBody(newBlog.getBody());
			blog.setCategoryKey(newBlog.getCategoryKey());
		}
		finally {
			pm.close();
		}
	}

	@Override
	public void deleteBlog(String blogKey) {
		// TODO Auto-generated method stub
		Key key = KeyFactory.stringToKey(blogKey);
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		try {
			Blog blog = pm.getObjectById(Blog.class, key);
			pm.deletePersistent(blog);
		}
		finally {
			pm.close();
		}
	}

	@Override
	public int getBlogNumber() {
		String query = "select count(this) from "+Blog.class.getName();
		PersistenceManager pm = persistenceManagerFactory.getPersistenceManager();
		try {
			Integer number = (Integer)pm.newQuery(query).execute();
			return number.intValue();
		}
		finally {
			pm.close();
		}
	}
	
	@Override
	public List<Blog> getBlogsByCategory(String categoryKey) {
		return null;
	}
}
